package com.xjzd.std.concurrency;

public class ThreadJoin {

    public static void main(String[] args) throws InterruptedException {
        // Thread的非静态方法
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("t1: " + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println("t2: " + i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        t1.start();
        t2.start();

        for (int i = 0; i < 10; i++) {
            System.out.println("main: " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            if (i == 1) {
                // 等待期间t1结束，当前线程变为就绪状态
                t1.join();
            }
        }
    }
}
